package cn.neusoft.luofa603.yuejian_29.activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.bean.Course;
import cn.neusoft.luofa603.yuejian_29.model.BuyModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BuyCourseActivity extends AppCompatActivity {
    private String participantname,course_id,participanttel,adultnum,pay,SessionID;
    private TextView cname,type,course_price,buy_num,totalpay,tpay;
    private EditText buname,btel;
    private ImageView alipay,weixinpay,bankcardpay;
    private int num,c_price;
    private Double tprice;
    private Button go_back,bt_less,bt_more;
    private Course course;
    private String price;
    private static final String PAY_ALIPAY="1",PAY_WEIXIN="2",PAY_card="3";

//    private ActivityBuyBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buycourse);
//        binding = DataBindingUtil.setContentView(this,R.layout.activity_buycourse);
        initViews();
        initEvents();
        initData();
    }
    public void initViews(){
        go_back=(Button)findViewById(R.id.go_back);
        bt_less=(Button)findViewById(R.id.bt_less);
        bt_more=(Button)findViewById(R.id.bt_more);
        alipay=(ImageView)findViewById(R.id.alipay);
        weixinpay=(ImageView)findViewById(R.id.weixinpay);
        bankcardpay=(ImageView)findViewById(R.id.bankcardpay);
        cname=(TextView)findViewById(R.id.cname);
        type=(TextView)findViewById(R.id.type);
        course_price=(TextView)findViewById(R.id.course_price);
        buname=(EditText) findViewById(R.id.buname);
        btel=(EditText) findViewById(R.id.btel);
        buy_num=(TextView)findViewById(R.id.buy_num);
        totalpay=(TextView)findViewById(R.id.totalpay);
//        tpay=(TextView)findViewById(R.id.tpay);

    }
    public void initEvents(){

        go_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        bt_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num>0){
                    num++;
                }
                buy_num.setText(Integer.toString(num));
                String cnum = buy_num.getText().toString();
                num = Integer.parseInt(cnum);
                Double numb = Double.parseDouble(cnum);
                tprice = numb*c_price;
                totalpay.setText(tprice.toString());

            }
        });
        bt_less.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num>1){
                    num--;
                }else{
                    Toast.makeText(BuyCourseActivity.this,"购买数量不能低于1！",Toast.LENGTH_SHORT).show();
                }
                buy_num.setText(Integer.toString(num));
                String cnum = buy_num.getText().toString();
                num = Integer.parseInt(cnum);
                Double numb = Double.parseDouble(cnum);
                tprice = numb*c_price;
                totalpay.setText(tprice.toString());
            }
        });
        alipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //Toast.makeText(BuyCourseActivity.this,"11111",Toast.LENGTH_LONG).show();

                dobuy(PAY_ALIPAY,"pppppp");
            }
        });
        bankcardpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dobuy(PAY_card,"pppppp");
            }
        });
        weixinpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dobuy(PAY_WEIXIN,"pppppp");;
            }
        });
    }
    public void initData(){
        getCourse();
        setData();
        readsessionid();
    }
    public void getCourse(){
        Bundle bundle = getIntent().getExtras();
        course =(Course) bundle.getSerializable("Course");
        course_id=(course.getId());
        cname.setText(course.getCourse_name());
        price=(course.getCourse_price());
        type.setText(course.getBmnotice());

    }
    public void setData(){
        participantname = buname.getText().toString();
        participanttel = btel.getText().toString();
        buy_num.setText("1");
        course_price.setText(price.toString());
        String cnum = buy_num.getText().toString();
        num = Integer.parseInt(cnum);
        c_price = Integer.parseInt(price);
        Double numb = Double.parseDouble(cnum);
        tprice = numb*c_price;
        pay = tprice.toString();
        adultnum = numb.toString();

    }
    public void readsessionid(){
        SharedPreferences sp= getSharedPreferences("sessionid", Context.MODE_PRIVATE);
        SessionID=sp.getString("sessionid", null);
       // System.out.println("111111111111111111"+SessionID);
    }

    public void dobuy(String paytype,String paynotice){
        BuyModel buyModel=new BuyModel();
        Call<String> call = buyModel.buy("signcourse",participantname,course_id,participanttel,adultnum,paytype,pay,paynotice,SessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String result= response.body();

                if(result==null){
                    Toast.makeText(BuyCourseActivity.this,"联网失败！",Toast.LENGTH_LONG).show();
                }else{

                    if (result.isEmpty()){

                        Toast.makeText(BuyCourseActivity.this,"购买失败！",Toast.LENGTH_LONG).show();
                    }else{

                        Toast.makeText(BuyCourseActivity.this,"购买成功！",Toast.LENGTH_LONG).show();


                    }

                    }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });



    }
}
